﻿Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
'----------加密解密

Public Class Cryp
#Region "MD5加密"
    Public Function MD5(ByVal s As String) As String
        Try
            Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(s)
            Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
            Dim ATR As String = ""
            Dim i As Integer
            For i = 0 To 15
                ATR &= Hex(hashvalue(i)).PadLeft(2, CChar("0")).ToLower
            Next
            dataToHash = Nothing : hashvalue = Nothing : i = Nothing
            Return ATR
        Catch ex As Exception
            Return ""
        End Try
    End Function
#End Region
#Region "DES加密"
    Public Function desencryp(ByVal pToEncrypt As String, ByVal sKey As String) As String
        Dim s As String = ""
        Try
            Dim des As DESCryptoServiceProvider
            des = New DESCryptoServiceProvider
            Dim inputByteArray() As Byte
            inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt)
            Dim key As Byte() = New Byte(7) {}
            Dim key2 As Byte() = Encoding.UTF8.GetBytes(sKey)
            If key2.Length < 1 Then
                key2 = Encoding.UTF8.GetBytes("12345678")
            End If
            If key2.Length > 8 Then
                key2 = Encoding.UTF8.GetBytes(Left(sKey, 8))
            End If
            For i1 As Integer = 0 To key2.Length - 1
                key(i1) = key2(i1)
            Next
            des.Key = key
            des.IV = New Byte(key.Length - 1) {}

            Using ms As New MemoryStream()
                Dim cs As New CryptoStream(ms, des.CreateEncryptor, CryptoStreamMode.Write)
                cs.Write(inputByteArray, 0, inputByteArray.Length)
                cs.FlushFinalBlock()
                Dim ret As New StringBuilder()
                Dim b As Byte
                For Each b In ms.ToArray()
                    ret.AppendFormat("{0:X2}", b)
                Next
                s = ret.ToString()
            End Using
        Catch ex As Exception
            s = ""
        End Try
        Return s
    End Function
#End Region
#Region "DES解密"
    Public Function desdecryp(ByVal pToDecrypt As String, ByVal sKey As String) As String
        Dim s As String = ""
        Try
            Using des As New DESCryptoServiceProvider()
                Dim len As Integer
                len = CInt(pToDecrypt.Length / 2 - 1)
                Dim inputByteArray(len) As Byte
                Dim x As Integer
                Dim i As Integer
                For x = 0 To len
                    i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)
                    inputByteArray(x) = CType(i, Byte)
                Next
                Dim key As Byte() = New Byte(7) {}
                Dim key2 As Byte() = Encoding.UTF8.GetBytes(sKey)
                If key2.Length < 1 Then
                    key2 = Encoding.UTF8.GetBytes("12345678")
                End If
                If key2.Length > 8 Then
                    key2 = Encoding.UTF8.GetBytes(Left(sKey, 8))
                End If
                For i1 As Integer = 0 To key2.Length - 1
                    key(i1) = key2(i1)
                Next
                des.Key = key
                des.IV = New Byte(key.Length - 1) {}
                Dim ms As MemoryStream
                ms = New MemoryStream
                Dim cs As New CryptoStream(ms, des.CreateDecryptor, CryptoStreamMode.Write)
                cs.Write(inputByteArray, 0, inputByteArray.Length)
                cs.FlushFinalBlock()
                s = Encoding.UTF8.GetString(ms.ToArray)
            End Using
        Catch ex As Exception
            s = ""
        End Try
        Return s
    End Function
#End Region
End Class
